﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace ProjManagSolution.Web
{
    /// <summary>
    /// Summary description for receiver
    /// </summary>
    public class receiver : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string filename = context.Request.QueryString["filename"].ToString();
            string foldername = context.Request.QueryString["projectid"].ToString();

            string path = "~/Uploaded/" + foldername;
            string mappedPath = System.Web.Hosting.HostingEnvironment.MapPath(path);
            if (!Directory.Exists(mappedPath))
                Directory.CreateDirectory(mappedPath);

            using (FileStream fs = File.Create(context.Server.MapPath(path + "/" + filename)))
            {
                SaveFile(context.Request.InputStream, fs);
            }
        }

        private void SaveFile(Stream stream, FileStream fs)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {
                fs.Write(buffer, 0, bytesRead);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}